package com.jeff.android.lab.module.anim.path;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * @author: duff
 * @date: 2019/1/3
 * @since: 1.0.0
 */
public class CottonView extends View implements Wave {
    private Paint mPaint;
    private Path mPath;

    private Paint mCottonPaint;
    private Path mCottonPath;

    private int mWidth = 0;
    private int mHeight = 0;


    private ValueAnimator mValueAnimator;

    public CottonView(Context context) {
        super(context);
        init();
    }

    public CottonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CottonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
//        mPaint.setColor(Color.parseColor("#00aff9"));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(5);

        RadialGradient radialGradient = new RadialGradient(0, 0, 300, Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
        mPaint.setShader(radialGradient);

        mPath = new Path();


        mCottonPaint = new Paint();
        mCottonPaint.setColor(Color.parseColor("#ff0000"));
        mCottonPaint.setStyle(Paint.Style.STROKE);
        mCottonPaint.setStrokeWidth(5);

        mCottonPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth() / 2, getHeight() / 2);

        drawCircle(canvas);
    }

    private void drawWave(Canvas canvas) {
        mPath.reset();

        mPath.lineTo(getWidth(), getHeight());
        mPath.lineTo(0, getHeight());
        mPath.close();

        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public void start() {

    }

    @Override
    public void end() {

    }

    @Override
    public void setPercent(float percent) {

    }

    private void drawCircle(Canvas canvas) {
        mPath.addCircle(0, 0, 300, Path.Direction.CW);
        canvas.drawPath(mPath, mPaint);

        mCottonPath.moveTo(-320, 0);
//        mCottonPath.quadTo(-600, -600, 0, -320);
//        mCottonPath.lineTo(0, -320);
//        mCottonPath.quadTo(-500, 500, 0, -330);
        mCottonPath.rQuadTo(-20, -300, 320, -330);

        Math.sin(60);

        canvas.drawPath(mCottonPath, mCottonPaint);
    }

    private void startAnim() {
        mValueAnimator = ValueAnimator.ofFloat(0, 1.0f);
        mValueAnimator.setDuration(1000);
        mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mValueAnimator.setInterpolator(new LinearInterpolator());
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

            }
        });

        mValueAnimator.start();
    }


}
